//
//  main.m
//  Demo0026
//
//  Created by zTercel on 13-3-8.
//  Copyright (c) 2013年 zTercel. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        // archive
        {
            NSMutableArray* array = [NSMutableArray arrayWithObjects:@"maLiLi", @"liLei", @"hanMeiMei", @"jim", @"Joy", nil];
            [array addObject:@"ztercel"];
            [array writeToFile:@"array.data" atomically:YES];
            
            NSString* string = [NSString stringWithFormat:@"This year is %u", 2013];
            [string writeToFile:@"string.data" atomically:YES encoding:NSASCIIStringEncoding error:nil];
            
            NSMutableDictionary* dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"18", @"maLili",@"19", @"liLei", @"17", @"haMeiMei", @"18", @"jim", @"19", @"Joy", nil];
            [dictionary setObject:@"20" forKey:@"ztercel"];
            [dictionary writeToFile:@"dictionary.data" atomically:YES];
            
            NSURL* url = [NSURL URLWithString:@"http://www.baidu.com"];
            NSData* data = [NSData dataWithContentsOfURL:url];
            [data writeToFile:@"data.data" atomically:YES];
        }
        
        // unarchive
        {
            NSMutableArray* array = [NSMutableArray arrayWithContentsOfFile:@"array.data"];
            NSLog(@"\narray data:");
            for (int ii = 0; ii < [array count]; ii++) {
                NSLog(@"%@", [array objectAtIndex:ii]);
            }
            
            NSString* string = [NSString stringWithContentsOfFile:@"string.data" encoding:NSASCIIStringEncoding error:nil];
            NSLog(@"\nstring data: \n %@", string);
            
            NSMutableDictionary* dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:@"dictionary.data"];
            NSLog(@"\ndictionary data: ");
            for (NSString* key in dictionary){
                NSLog(@"%@ = %@", key, [dictionary objectForKey:key]);
            }
            
            NSData* data = [NSData dataWithContentsOfFile:@"data.data"];
            NSLog(@"\ndata data: ");
            
            NSString* content = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@", content);
        }
        
        NSFileManager* fm = [NSFileManager defaultManager];

        NSArray* paths = [NSArray arrayWithObjects:@"array.data", @"string.data", @"dictionary.data", @"data.data", nil];
        for (NSString* path in paths)
        {
            if ([fm fileExistsAtPath:path])
            {
                [fm removeItemAtPath:path error:nil];
                NSLog(@"delete %@ file successfully", path);
            }
        }

    }
    return 0;
}

